<?php

// $Id: pgsql.php 2175 2009-02-02 06:24:38Z yangyi $

/**
 * 定义 QDB_Adapter_Pdo_Mysql 类
 *
 * @link http://qeephp.com/
 * @copyright Copyright (c) 2006-2009 Qeeyuan Inc. {@link http://www.qeeyuan.com}
 * @license New BSD License {@link http://qeephp.com/license/}
 * @version $Id: pgsql.php 2175 2009-02-02 06:24:38Z yangyi $
 * @package database
 */

/**
 * QDB_Adapter_Pdo_Mysql 类提供对 PDO_MySQL 的支持
 *
 * @author yangyi.cn.gz@gmail.com
 * @version $Id: pgsql.php 2175 2009-02-02 06:24:38Z yangyi $
 * @package database
 */
class QDB_Adapter_Pdo_Mysql extends QDB_Adapter_Pdo_Abstract {

    protected $_pdo_type = 'mysql';
    protected $_bind_enabled = true;
    protected $_affected_rows = 0;

    function connect() {

        return parent::connect();
    }

    function qstr($str) {
        return $str;
    }

    function identifier($name) {
        return $name;
    }

    function nextID($table_name, $field_name, $start_value = 1) {
        return $this->_conn->lastInsertId() + 1;
    }

    function createSeq($seq_name, $start_value = 1) {
        
    }

    function dropSeq($seq_name) {
        
    }

    function insertID() {
        return $this->_conn->lastInsertId();
    }

    function metaColumns($table_name) {
        static $type_mapping = array(
            'bit' => 'int1',
            'tinyint' => 'int1',
            'bool' => 'bool',
            'boolean' => 'bool',
            'smallint' => 'int2',
            'mediumint' => 'int3',
            'int' => 'int4',
            'integer' => 'int4',
            'bigint' => 'int8',
            'float' => 'float',
            'double' => 'double',
            'doubleprecision' => 'double',
            'float unsigned' => 'float',
            'decimal' => 'dec',
            'dec' => 'dec',
            'date' => 'date',
            'datetime' => 'datetime',
            'timestamp' => 'timestamp',
            'time' => 'time',
            'year' => 'int2',
            'char' => 'char',
            'nchar' => 'char',
            'varchar' => 'varchar',
            'nvarchar' => 'varchar',
            'binary' => 'binary',
            'varbinary' => 'varbinary',
            'tinyblob' => 'blob',
            'tinytext' => 'text',
            'blob' => 'blob',
            'text' => 'text',
            'mediumblob' => 'blob',
            'mediumtext' => 'text',
            'longblob' => 'blob',
            'longtext' => 'text',
            'enum' => 'enum',
            'set' => 'set'
        );

        $rs = $this->execute(sprintf('SHOW FULL COLUMNS FROM %s', $this->qid($table_name)));

        $retarr = array();
        $rs->fetch_mode = QDB::FETCH_MODE_ASSOC;
        $rs->result_field_name_lower = true;

        while (($row = $rs->fetchRow())) {
            $field = array();
            $field['name'] = $row['field'];
            $type = strtolower($row['type']);

            $field['scale'] = null;
            $query_arr = false;
            if (preg_match('/^(.+)\((\d+),(\d+)/', $type, $query_arr)) {
                $field['type'] = $query_arr[1];
                $field['length'] = is_numeric($query_arr[2]) ? $query_arr[2] : - 1;
                $field['scale'] = is_numeric($query_arr[3]) ? $query_arr[3] : - 1;
            } elseif (preg_match('/^(.+)\((\d+)/', $type, $query_arr)) {
                $field['type'] = $query_arr[1];
                $field['length'] = is_numeric($query_arr[2]) ? $query_arr[2] : - 1;
            } elseif (preg_match('/^(enum)\((.*)\)$/i', $type, $query_arr)) {
                $field['type'] = $query_arr[1];
                $arr = explode(",", $query_arr[2]);
                $field['enums'] = $arr;
                $zlen = max(array_map("strlen", $arr)) - 2; // PHP >= 4.0.6
                $field['length'] = ($zlen > 0) ? $zlen : 1;
            } else {
                $field['type'] = $type;
                $field['length'] = - 1;
            }

            $field['ptype'] = $type_mapping[strtolower($field['type'])];
            $field['not_null'] = (strtolower($row['null']) != 'yes');
            $field['pk'] = (strtolower($row['key']) == 'pri');
            $field['auto_incr'] = (strpos($row['extra'], 'auto_incr') !== false);
            if ($field['auto_incr']) {
                $field['ptype'] = 'autoincr';
            }
            $field['binary'] = (strpos($type, 'blob') !== false);
            $field['unsigned'] = (strpos($type, 'unsigned') !== false);

            $field['has_default'] = $field['default'] = null;
            if (!$field['binary']) {
                $d = $row['default'];
                if (!is_null($d) && strtolower($d) != 'null') {
                    $field['has_default'] = true;
                    $field['default'] = $d;
                }
            }

            if ($field['type'] == 'tinyint' && $field['length'] == 1) {
                $field['ptype'] = 'bool';
            }

            $field['desc'] = !empty($row['comment']) ? $row['comment'] : '';
            if (!is_null($field['default'])) {
                switch ($field['ptype']) {
                    case 'int1':
                    case 'int2':
                    case 'int3':
                    case 'int4':
                        $field['default'] = intval($field['default']);
                        break;
                    case 'float':
                    case 'double':
                    case 'dec':
                        $field['default'] = doubleval($field['default']);
                        break;
                    case 'bool':
                        $field['default'] = (bool) $field['default'];
                }
            }

            $retarr[strtolower($field['name'])] = $field;
        }
        return $retarr;
    }

    function metaTables($pattern = null, $schema = null) {
        $sql = 'SHOW TABLES';
        if ($schema != '') {
            $sql .= " FROM `{$schema}`";
        }
        if ($pattern != '') {
            $sql .= ' LIKE ' . $this->qstr($pattern);
        }
        return $this->getCol($sql);
    }

}
